package com.prs.crm.action.cqunicom;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import com.prs.crm.annotation.Clew;
import com.prs.crm.context.AppContext;
import com.prs.crm.domain.cqunicom.Film;
import com.prs.crm.domain.cqunicom.Movie;
import com.prs.crm.domain.hr.Person;



import com.prs.crm.exception.InValidDeleteException;


import com.prs.crm.service.cqunicom.FilmService;
import com.prs.crm.service.cqunicom.MovieService;
import com.prs.crm.service.hr.PersonService;


import com.prs.crm.service.sys.LogService;

/**
 * @author  Wangting
 *
 * 创建时间：2011-3-22
 * 
 */


public class FilmAction extends BaseAction {
	private static final long serialVersionUID = 1L;

	private Film film;
	private FilmService  filmService;
    private  Integer moid; 
	private Integer[] filmIds;
	private Movie movie;
	private MovieService  movieService;
	private String movieName; 
	
	private PaginationSupport pagination;
	private LogService logService;
	private Person person;
	private PersonService  personService;
	
	@Action("editFilm")
	public String edit() {
		
		 movieName=AppContext.getAccountContext().currentUser().getLoginCode();
		
		return SUCCESS;
	}
	
	@Action("viewFilm")
	public String view() {
		return SUCCESS;
	}
	
	@Action("listFilm")
	public String listMovie() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				Film.class);
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getFilmService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}
	@Action("selectFilm")
	public String selectFilm() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(Film.class);		
		Movie  mo=this.getMovieService().get(moid);
		criteria.add(Restrictions.eq("movieA", mo));
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getFilmService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}
	
	@Action("alterFilm")
	public String alterMovie() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				Film.class);
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getFilmService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}
	
	@Clew(results = { "success", "none" })
	@Action(value = "saveFilm", results = {
			@Result(name = "success", type = "redirect", location = "editFilm"),
			@Result(name = "none", type = "redirect", location = "listFilm"),
			@Result(name = "error", type = "chain", location = "editFilm") })
	public String save() {

//		   film.setMovieA(AppContext.getAccountContext().getUser().getMovie());
		   this.getFilmService().save(film);
		   this.setPrompt("影片录入成功");
		   return SUCCESS;
	}

	@Action(value = "removeFilm", results = {
			@Result(name = "success", type = "redirect", location = "listFilm"),
			@Result(name = "error", type = "chain", location = "listFilm") })
	public String remove() {
		try {
			if (filmIds == null) {
				this.addActionError("请选择要删除的电影院");
				return ERROR;
			}
			this.getFilmService().batchRemove(filmIds);
			return SUCCESS;
		} catch (InValidDeleteException e) {
			return ERROR;
		}
	}
	
	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}

	
	public PaginationSupport getPagination() {
		return pagination;
	}
	public LogService getLogService() {
		return logService;
	}
	public void setLogService(LogService logService) {
		this.logService = logService;
	}

	
	
	public Integer getMoid() {
		return moid;
	}

	public void setMoid(Integer moid) {
		this.moid = moid;
	}

	public static long getSerialversionuid() {
		return serialVersionUID;
	}

	public static long getSerialVersionUID() {
		return serialVersionUID;
	}
	public boolean istel(String str) {
		boolean tag = false;  
		final String pattern1 = "^(([0-9]{3,4})|[0-9]{3,4}-)?[0-9]{7,8}$";
		final Pattern pattern = Pattern.compile(pattern1);
		final Matcher mat = pattern.matcher(str);
		if (!mat.find()) {
			tag = true;
		}
		return tag;
	}

	public boolean isnull(String str) {
		boolean s=true;
		if (str.trim().equals("") || str == null) {
			s= false;
		}
		return s;
	}

	public Film getFilm() {
		return film;
	}

	public void setFilm(Film film) {
		this.film = film;
	}

	public FilmService getFilmService() {
		return filmService;
	}

	public void setFilmService(FilmService filmService) {
		this.filmService = filmService;
	}

	public Integer[] getFilmIds() {
		return filmIds;
	}

	public void setFilmIds(Integer[] filmIds) {
		this.filmIds = filmIds;
	}

	public Movie getMovie() {
		return movie;
	}

	public void setMovie(Movie movie) {
		this.movie = movie;
	}

	public MovieService getMovieService() {
		return movieService;
	}

	public void setMovieService(MovieService movieService) {
		this.movieService = movieService;
	}

	public String getMovieName() {
		return movieName;
	}

	public void setMovieName(String movieName) {
		this.movieName = movieName;
	}

	public Person getPerson() {
		return person;
	}

	public void setPerson(Person person) {
		this.person = person;
	}

	public PersonService getPersonService() {
		return personService;
	}

	public void setPersonService(PersonService personService) {
		this.personService = personService;
	}

	
	
	

}
